"use strict";



var curDate = function(){
	var today = new Date();
	var dd = today.getDate();
	var mm = today.getMonth()+1; //January is 0!
	var yyyy = today.getFullYear();

	if(dd<10) {
	    dd='0'+dd
	} 

	if(mm<10) {
	    mm='0'+mm
	} 

	today = yyyy + '-' + mm + '-' + dd;
	return today;
};



var curDateTime = function() {
    var now  = new Date();
    var dd = now.getDate();
    var mm = now.getMonth() + 1;
    var yyyy = now.getFullYear();
    var hh = now.getHours();
    var ii = now.getMinutes();
    var ss = now.getSeconds();
    dd = dd < 10 ? '0'+dd : dd;
	mm = mm < 10 ? '0'+mm : mm;
    hh = hh < 10 ? '0'+hh : hh;
    ii = ii < 10 ? '0'+ii : ii;
    ss = ss < 10 ? '0'+ss : ss;
    now = yyyy + '-' + mm + '-' + dd + " " + hh + ":" + ii + ":" + ss;
    return now;
};


var getISODate = function(date){
	var dd = date.getDate();
	var mm = date.getMonth()+1; //January is 0!
	var yyyy = date.getFullYear();

	if(dd<10) {
	    dd='0'+dd
	} 

	if(mm<10) {
	    mm='0'+mm
	} 

	date = yyyy + '-' + mm + '-' + dd;
	return date;
};

var getISODatetime = function(date){
    if (date == null || date == undefined || isNaN(date.getTime()) ){
        return '';
    }   

    var dd = date.getDate();
    var mm = date.getMonth()+1; //January is 0!
    var yyyy = date.getFullYear();
    var hh = date.getHours();
    var ii = date.getMinutes();
    var ss = date.getSeconds();

    if(dd<10) {
        dd='0'+dd
    } 

    if(mm<10) {
        mm='0'+mm
    } 
    hh = hh < 10 ? '0'+hh : hh;
    ii = ii < 10 ? '0'+ii : ii;
    ss = ss < 10 ? '0'+ss : ss;

    date = yyyy + '-' + mm + '-' + dd + ' ' + hh + ':' + ii + ':' + ss;
    return date;
};


var getISOMonth = function(date){
    var mm = date.getMonth()+1; //January is 0!
    var yyyy = date.getFullYear();

    if(mm<10) {
        mm='0'+mm
    } 

    date = yyyy + '年' + mm + '月';
    return date;
};

var getDateObj = function(date) {
	var reg = new RegExp(/(\d{4})\-(\d{2})\-(\d{2})/i);
	var arr = reg.exec(date);
	if (arr === null) {
		return new Date();
	} else {
		return new Date(parseInt(arr[1]), parseInt(arr[2])-1, parseInt(arr[3]));
	}
};

var getDateStartEndByType = function(date = Date(), type, num = 0) {
    if (type == 'WEEK') {
        var startDate = new Date(date);
        var endDate = new Date(date);
        var day = date.getDay();
        
        day = (day == 0 ? 7 : day);
        startDate.setDate(startDate.getDate() - num * 7 - day + 1);
        endDate.setDate(endDate.getDate() + 7 - day);
    } else if (type == 'MONTH') {
        var startDate = new Date(date.getFullYear(), date.getMonth() - num, 1);
        var endDate = new Date(date.getFullYear(), date.getMonth() + 1, 1);

        endDate.setDate(0);
    } else if (type == 'QUARTER') {
        var startDate = new Date(date.getFullYear(), (Math.floor(date.getMonth()/3)-num)*3, 1);
        var endDate = new Date(date.getFullYear(), (Math.floor(date.getMonth()/3)+1)*3, 1);
        
        endDate.setDate(0);
    } else if (type == 'YEAR') {
        var startDate = new Date(date.getFullYear() - num, 0, 1);
        var endDate = new Date(date.getFullYear() + 1, 0, 1);
        
        endDate.setDate(0);
    }
    return {'startdate': startDate, 'enddate': endDate};
}

const getWeekOfMonth = function(day) {
    var day1 = day.getDay();
    day1 = day1 == 0 ? 7 : day1;
    var date = day.getDate();
    return Math.ceil((date + 7 - day1) / 7);

    /*var exact = true;
    var month = day.getMonth()
        , year = day.getFullYear()
        , firstWeekday = new Date(year, month, 1).getDay()
        , lastDateOfMonth = new Date(year, month + 1, 0).getDate()
        , offsetDate = day.getDate() + firstWeekday - 1
        , index = 1 // start index at 0 or 1, your choice
        , weeksInMonth = index + Math.ceil((lastDateOfMonth + firstWeekday - 7) / 7)
        , week = index + Math.floor(offsetDate / 7)
    ;
    if (exact || week < 2 + index) return week;
    return week === weeksInMonth ? index + 5 : week;*/
};

/**
 * 判断年份是否为润年
 *
 * @param {Number} year
 */
var isLeapYear = function (year) {
    return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
/**
 * 获取某一年份的某一月份的天数
 *
 * @param {Number} year
 * @param {Number} month
 */
var getMonthDays = function(year, month) {
    return [31, null, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month] || (isLeapYear(year) ? 29 : 28);
}
 /**
 * 获取某年的某天是第几周
 * @param {Number} y
 * @param {Number} m
 * @param {Number} d
 * @returns {Number}
 */
var getWeekNumber = function(date) {
    var year = date.getFullYear(),
        month = date.getMonth(),
        days = date.getDate();
    //那一天是那一年中的第多少天
    for (var i = 0; i < month; i++) {
        days += getMonthDays(year, i);
    }

    //那一年第一天是星期几
    var yearFirstDay = new Date(year, 0, 1).getDay() || 7;

    var week = null;
    if (yearFirstDay == 1) {
        week = Math.ceil(days / yearFirstDay);
    } else {
        days -= (7 - yearFirstDay + 1);
        week = Math.ceil(days / 7); //那边标准是第一年的第一个星期一作为第一周
    }

    return week;
}

const getQuarter = function (d) {
    d = d || new Date();
    var month = d.getMonth();
    if(month >= 0 && month < 3){
        return 1
    }
    else if(month >= 3 && month < 6){
        return 2
    }
    else if(month >= 6 && month < 9){
        return 3
    }
    else if(month >= 9 && month < 12){
        return 4
    }
}

export { curDate, curDateTime, getISODate, getISOMonth, getDateObj, getISODatetime, getDateStartEndByType, getWeekOfMonth};
export { getQuarter, getWeekNumber };